<template>
  <div class="box">
    <header class="header">
      <van-nav-bar
        title="地址管理"
        left-arrow
        @click-left="$router.back()"
      />
    </header>
    <div class="content">
          <van-address-list
            v-model="chosenAddressId"
            :list="list"
            default-tag-text="默认"
            @add="onAdd"
            @edit="onEdit"
            @select = "onSelect"
          />
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import Cookies from 'js-cookie'
import { AddressList, SwipeCell, Toast } from 'vant'
import { getAddress } from '../../api/index.js'

Vue.use(AddressList)
Vue.use(SwipeCell)
Vue.use(Toast)
export default {
  data () {
    return {
      chosenAddressId: '1',
      list: [],
      AddressList: []
    }
  },
  methods: {
    onAdd () {
      this.$router.push('/setAddress')
    },
    onEdit (item, index) {
      this.$router.push('/setAddress')
    },
    onSelect (item, index) {
      Toast({
        message: '修改成功',
        onClose: () => {
          this.$router.push('/order')
        }
      })
      // console.log(item.id)
      // updateState({
      //   userId: Cookies.get('userId'),
      //   addressId: item.id
      // }).then(() => {
      //   console.log('数据修改成功')
      // })
    }
  },
  mounted () {
    // 获取地址数据
    getAddress({
      userId: Cookies.get('userId')
    }).then(res => {
      const arr = res.data.data
      console.log(arr)
      arr.forEach((val, idx) => {
        this.list.push({
          id: val.addressId,
          name: val.userMsg[0].name,
          tel: val.userMsg[0].tell,
          address: val.userMsg[0].province + val.userMsg[0].city + val.userMsg[0].country + val.userMsg[0].addressDetail,
          isDefault: val.userMsg[0].isDefault
        })
      })
    })
  }
}
</script>

<style>
</style>
